package model.enemies;

import java.util.concurrent.CopyOnWriteArrayList;

import model.Factory;
import model.enums.Enemies;
import model.utils.Speed;
import android.graphics.Bitmap;
import controller.GameBoard;

/**
 * Thread creates 10 enemies with the correct enemy type, and depending on the
 * level upgrades them
 * 
 * @author EvilTeam
 * 
 */
public class CreateEnemiesThread extends Thread {
	private CopyOnWriteArrayList<IEnemy> enemies;
	private Bitmap image;
	private int height;
	private Thread predecessor;
	private int level;
	private static String nextEnemy;

	public CreateEnemiesThread(CopyOnWriteArrayList<IEnemy> enemies,
			Bitmap image, int height, Thread predecessor, int level) {
		this.enemies = enemies;
		this.image = image;
		this.height = height;
		this.predecessor = predecessor;
		this.level = level;
	}

	@Override
	public void run() {
		//When the game is start for a first time
		//wait for 10 seconds and then start the waves.
		if(this.level == 0){
			try {
				Thread.sleep(10000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		try {
			if (predecessor != null && predecessor.isAlive()) {
				predecessor.join();
				Thread.sleep(15000);
			}
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}

		GameBoard.setCurrentLevel(level);
		for (int i = 0; i < 10; i++) {
			GameBoard.setCurrentThread(this);
			setNextLevel();

			// create speed enemy
			if (this.level % 2 == 0 && this.level % 3 != 0
					&& this.level % 5 != 0) {
				IEnemy currentEnemy = Factory.getEnemy(Enemies.SPEED_ENEMY, 0,
						height / 1.75f, image);
				updateBetweenLevels(5, 10, currentEnemy, new Speed(15, 0), 160);
				updateBetweenLevels(10, 15, currentEnemy, new Speed(15, 0), 230);
				updateBetweenLevels(15, 25, currentEnemy, new Speed(15, 0), 330);
				enemies.add(currentEnemy);
			}
			// create boss
			else if (this.level % 5 == 0 && this.level != 0) {
				IEnemy currentEnemy = Factory.getEnemy(Enemies.BOSS, 0,
						height / 1.75f, image);
				updateBetweenLevels(5, 10, currentEnemy, new Speed(15, 0), 300);
				updateBetweenLevels(10, 15, currentEnemy, new Speed(15, 0), 450);
				updateBetweenLevels(16, 25, currentEnemy, new Speed(15, 0), 700);
				enemies.add(currentEnemy);
			}
			// create heavy enemy
			else if (this.level % 3 == 0 || this.level == 0) {
				IEnemy currentEnemy = Factory.getEnemy(Enemies.HEAVY_ENEMY, 0,
						height / 1.75f, image);
				updateBetweenLevels(5, 10, currentEnemy, new Speed(10, 0), 200);
				updateBetweenLevels(10, 15, currentEnemy, new Speed(11, 0), 270);
				updateBetweenLevels(15, 25, currentEnemy, new Speed(13, 0), 450);
				enemies.add(currentEnemy);
			}

			// create armored enemy
			else {
				IEnemy currentEnemy = Factory.getEnemy(Enemies.ARMORED_ENEMY,
						0, height / 1.75f, image);
				updateBetweenLevels(5, 10, currentEnemy, new Speed(10, 0), 215);
				updateBetweenLevels(10, 15, currentEnemy, new Speed(11, 0), 330);
				updateBetweenLevels(15, 20, currentEnemy, new Speed(13, 0), 430);
				enemies.add(currentEnemy);
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * Set what wave is coming next level.
	 */
	private void setNextLevel() {
		int nextlevel = this.level + 1;
		if (nextlevel % 2 == 0 && nextlevel % 3 != 0 && nextlevel % 5 != 0) {
			setNextEnemy("speed enemy");
		} else if (nextlevel % 5 == 0 && nextlevel != 0) {
			setNextEnemy("boss");
		} else if (nextlevel % 3 == 0 || nextlevel == 0) {
			setNextEnemy("heavy enemy");
		} else {
			setNextEnemy("armored enemy");
		}
	}

	private void updateBetweenLevels(int loweLevel, int higherLevel,
			IEnemy enemy, Speed speed, int health) {
		if (level > loweLevel && level <= higherLevel) {
			enemy.setHealthPoints(enemy.getHealthPoints() + health);
			enemy.setSpeed(speed);
		}
	}

	public String getCurrentEnemy() {
		return nextEnemy;
	}

	public void setNextEnemy(String currentEnemy) {
		if (currentEnemy != null)
			CreateEnemiesThread.nextEnemy = currentEnemy;
		else
			CreateEnemiesThread.nextEnemy = "";
	}
}
